Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business

Public Class CountryDetail1
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ltrMessage As System.Web.UI.WebControls.Literal
    Protected WithEvents txtCountryCode As System.Web.UI.WebControls.TextBox
    Protected WithEvents Requiredfieldvalidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtCountryName As System.Web.UI.WebControls.TextBox
    Protected WithEvents Requiredfieldvalidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents txtRoadWeight As System.Web.UI.WebControls.TextBox
    Protected WithEvents cboMeasUnit As System.Web.UI.WebControls.DropDownList
    Protected WithEvents chkShowOnIDSCart As System.Web.UI.WebControls.CheckBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected mainObj As Country = Nothing

    Protected Property ReferrerUrl() As String
        Get
            Return Convert.ToString(ViewState("ReferrerUrl"))
        End Get
        Set(ByVal Value As String)
            ViewState("ReferrerUrl") = Value
        End Set
    End Property

    Protected ReadOnly Property IsAddNew() As Boolean
        Get
            Return RequestContext.CountryID = 0
        End Get
    End Property

    Private Function BuildObject() As Boolean
        If IsAddNew Then
            mainObj = New Country
        Else
            mainObj = CountryService.GetCountry(RequestContext.CountryID)
            If mainObj Is Nothing Then
                Utilities.ShowMessagePage("Failed to load requested Country. It may be deleted or not found")
                Return False
            End If
        End If

        Return True
    End Function

    Private Sub UpdateData(ByVal bSave As Boolean)
        If (bSave) Then
            Try
                mainObj.CountryCode = txtCountryCode.Text
                mainObj.CountryName = txtCountryName.Text
                If (txtRoadWeight.Text.Length > 0) Then
                    mainObj.RoadWeight = Convert.ToDecimal(txtRoadWeight.Text)
                Else
                    mainObj.RoadWeight = Nulls.NullDecimal
                End If
                mainObj.MeasUnit = cboMeasUnit.SelectedItem.Value
                mainObj.IsShowOnIDSCart = chkShowOnIDSCart.Checked
            Catch ex As Exception
            End Try

        Else
            Try
                txtCountryCode.Text = mainObj.CountryCode
                txtCountryName.Text = mainObj.CountryName
                If (mainObj.RoadWeight <> Nulls.NullDecimal) Then
                    txtRoadWeight.Text = mainObj.RoadWeight.ToString
                End If
                If (mainObj.MeasUnit <> String.Empty) Then
                    cboMeasUnit.SelectedValue = mainObj.MeasUnit
                End If
                chkShowOnIDSCart.Checked = mainObj.IsShowOnIDSCart
            Catch ex As Exception
            End Try
        End If
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If RequestContext.CurrentUser.IS_ADMIN = False Then
            btnDelete.Visible = False
        End If

        If Not IsPostBack Then
            ReferrerUrl = Utilities.GetReferrerUrl("CountryList.aspx")
            If IsAddNew Then
                btnDelete.Visible = False
            Else
                If (BuildObject()) Then
                    UpdateData(False)
                End If

                ' TODO check in used

                btnDelete.Attributes("onclick") = "return confirm('Are you sure you want to delete?');"
                btnDelete.Visible = True
            End If
        End If
    End Sub


    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If (Page.IsValid) Then
            If (BuildObject()) Then             
                UpdateData(True)
                If IsAddNew Then                    
                    CountryService.CreateCountry(mainObj)
                Else
                    CountryService.UpdateCountry(mainObj)
                End If

                Response.Redirect(ReferrerUrl)
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect(ReferrerUrl)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        CountryService.DeleteCountry(RequestContext.CountryID)
        Response.Redirect(ReferrerUrl)
    End Sub

End Class
